<template>
	<view class="bg-white">
		<mysearch @onSearch="gotoSearch"></mysearch>
		<indexSwiper :swiperList="swiperList"></indexSwiper>
		<indexNav :navList="navList"></indexNav>
		<indexFloor :floorList="floorList"></indexFloor>
	</view>

</template>

<script setup>
	import {
		ref,
		computed,
		onMounted,
		onBeforeUnmount
	} from 'vue';

	import {
		onShow,
		onLoad
	} from "@dcloudio/uni-app"

	import {
		useCartStore
	} from "../../store";

	const cartStore = useCartStore()

	onShow(() => {
		uni.setTabBarBadge({
			index: 2,
			text: cartStore.total + ''
		})
	})

	const floorList = ref()
	const navList = ref()

	const swiperList = ref()

	const getData = async () => {
		const db = uniCloud.databaseForJQL()
		const swiper = await db.collection("swiper").where("status==true").field("image_src.url as url ,goods_id")
			.get()

		const nav = await db.collection("nav").where("status==true").field("image_src.url as url,name").get()

		// const floor = db.collection("floor").where("status==true").field(
		// 	"title_image.url as url,_id").getTemp()
		const floorTemp = db.collection("floor").where("status==true").getTemp()
		const productTemp = db.collection("product").where("status==true").field(
				"floor,image_src.url as url,image_width,query")
			.getTemp()

		const floor = await db.collection(floorTemp, productTemp).field(
				"title_image.url as url")
			.get()

		if (swiper.errCode != 0 || nav.errCode != 0 || floor.errCode != 0) {
			return uni.showToast({
				title: "数据加载有误"
			})
		}
		swiperList.value = swiper.data
		navList.value = nav.data
		floorList.value = floor.data

	}

	const gotoSearch = (e) => {
		uni.navigateTo({
			url: "/subpkg/search/search?placeholder=" + e.value
		})
	}

	onLoad(() => {
		getData()
	})
</script>

<style lang="scss">

</style>